﻿using System;
using System.Web;

namespace Regnemaskinen
{
    public class Clock : IHttpHandler
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            //write your handler implementation here.
        }

        #endregion

        bool IHttpHandler.IsReusable
        {
            get { return false; }
        }

        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            DateTime dt = DateTime.Now;
            /*
            String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
            String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
            String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
            String.Format("{0:h hh H HH}", dt);  // "4 04 16 16"      hour 12/24
            String.Format("{0:m mm}", dt);  // "5 05"            minute
            String.Format("{0:s ss}", dt);  // "7 07"            second
            String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
            String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
            String.Format("{0:t tt}", dt);  // "P PM"            A.M. or P.M.
            String.Format("{0:z zz zzz}", dt);  // "-6 -06 -06:00"   time zone
             * 
             * 
             * 
             * new Date(year, month, day, hours, minutes, seconds, milliseconds)
            */
            string s = String.Format("{0:HH mm ss}", dt);

            //string json3 = "{\"time\" : \"" + s + "\", \"milliseconds\" : " + dt.Millisecond + "}";

            string  json  = "{";
            json += "\"Year\":" + dt.Year + ",";
            json += "\"Month\":" + dt.Month + ",";
            json += "\"Day\":" + dt.Day + ",";
            json += "\"Hour\":" + dt.Hour + ",";
            json += "\"Minute\":" + dt.Minute + ",";
            json += "\"Second\":" + dt.Second + ",";
            json += "\"Millisecond\":" + dt.Millisecond + "";
                    json += "}";

            context.Response.ContentType = "application/json";
            context.Response.Write(json);
        }
    }
}
